package com.example.fund.dialogFragment;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.example.fund.util.DatabaseHelper;
import com.example.fund.util.FundUtil1;
import com.example.fund.R;

import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class DialogFragmentEx extends DialogFragment
{
	private SQLiteDatabase db;
	private List<Map<String, Object>> list;
	private IndexSetDialogAdapter adapter;
	private ListView listView;
	private EditText txtNumber;
	private EditText txtName;
	private EditText txtDefDate;
	
	public DialogFragmentEx() {
		super();
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
    	getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    	View view = inflater.inflate(R.layout.index_set_dialog, null);
    	listView = (ListView)view.findViewById(R.id.listview);
    	
		DatabaseHelper helper = new DatabaseHelper(getActivity());
		db = helper.getWritableDatabase();
    	list = FundUtil1.getIndexSet();
		adapter = new IndexSetDialogAdapter(getActivity(), list);
    	
    	listView.setAdapter(adapter);

    	TextView btnAddIndex = (TextView)view.findViewById(R.id.btnAddIndex);
    	TextView btnApply = (TextView)view.findViewById(R.id.btnApply); 
    	TextView btnCancel = (TextView)view.findViewById(R.id.btnCancel); 
    	txtNumber = (EditText)view.findViewById(R.id.txtNumber);
    	txtName = (EditText)view.findViewById(R.id.txtName);
    	txtDefDate = (EditText)view.findViewById(R.id.txtDefDate);

    	btnAddIndex.setOnClickListener(new OnClickListener(){
    		@Override
    		public void onClick(View arg0) {
    			btnAddIndex_onClick(arg0);
    		}});
    	btnApply.setOnClickListener(new OnClickListener(){
    		@Override
    		public void onClick(View arg0) {
    			btnApply_onClick(arg0);
    		}});
    	btnCancel.setOnClickListener(new OnClickListener(){
    		@Override
    		public void onClick(View arg0) {
    			btnCancel_onClick(arg0);
    		}});
    	this.setCancelable(false);// 设置点击屏幕Dialog不消失
    	return view;
		
/*		return super.onCreateView(inflater, container, savedInstanceState);*/
    }
    
	protected void btnCancel_onClick(View arg0) {
		this.dismiss();
	}

	protected void btnApply_onClick(View arg0) {
		FundUtil1.saveIndexSet(list);
		this.dismiss();
		getActivity().finish(); 
		Intent intent = new Intent(getActivity(), getActivity().getClass());  
		getActivity().startActivity(intent);
	}

	protected void btnAddIndex_onClick(View arg0) {
		if(txtNumber==null || txtNumber.getText()==null || txtNumber.getText().toString().trim().length()==0
				|| txtName==null || txtName.getText()==null || txtName.getText().toString().trim().length()==0)
		{
			return;
		}
		
		String number = txtNumber.getText().toString().trim();
		String name = txtName.getText().toString().trim();
		String defDate = txtDefDate.getText().toString().trim();
		
		String regex = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
		if(defDate!=null && defDate.trim().length()>0 && !Pattern.matches(regex, defDate))
		{
			return;
		}
		
		if(list==null)
		{
			list = new LinkedList<Map<String,Object>>();
		}
		for(int i=0;i<list.size();i++)
		{
			Object numberObj1 = list.get(i).get("number");
			if(number.equals(numberObj1.toString()))
			{
				list.get(i).put("name", name);
				list.get(i).put("defDate", defDate);
				adapter.notifyDataSetChanged();
				txtNumber.setText("");
				txtName.setText("");
				txtDefDate.setText("");
				return;
			}
		}
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("seq", 0);  
		map.put("number",number);
		map.put("name", name);  
		map.put("enable",1); 
		map.put("defDate",defDate); 
		
        list.add(map);
        adapter.notifyDataSetChanged();

		txtNumber.setText("");
		txtName.setText("");
		txtDefDate.setText("");
	}
	
	
	public class IndexSetDialogAdapter extends BaseAdapter
	{
		private LayoutInflater mInflater; 
		private List<Map<String,Object>> list;
		

		public IndexSetDialogAdapter(Context context,List<Map<String,Object>> list){
			this.mInflater = LayoutInflater.from(context);
			this.list = (list==null?new LinkedList<Map<String,Object>>():list);
		} 
		
		@Override
		public int getCount() { 
			return list==null?0:list.size(); 
		} 

		@Override
		public Object getItem(int arg0) { 
			return list.get(arg0); 
		} 

		@Override
		public long getItemId(int arg0) { 
			return 0; 
		} 

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) 
		{
			Map<String,Object> map = (Map<String,Object>)list.get(position);
			ViewHolder holder = null; 
			if (convertView == null) { 
				holder=new ViewHolder();   
				convertView = mInflater.inflate(R.layout.index_set_dialog_item, null); 
				holder.seq = (TextView)convertView.findViewById(R.id.seq); 
				holder.number = (TextView)convertView.findViewById(R.id.number);
				holder.name = (TextView)convertView.findViewById(R.id.name); 
				holder.defDate = (TextView)convertView.findViewById(R.id.defDate); 
				holder.refNumber = (TextView)convertView.findViewById(R.id.refNumber); 
				holder.moveup = (TextView)convertView.findViewById(R.id.moveup); 
				holder.movedown = (TextView)convertView.findViewById(R.id.movedown); 
				holder.del = (TextView)convertView.findViewById(R.id.del); 
				convertView.setTag(holder); 
			}else { 

				holder = (ViewHolder)convertView.getTag(); 
			} 


			holder.seq.setText((position+1)+"");
			holder.number.setText(map.get("number")+""); 
			holder.name.setText(map.get("name")+""); 
			if(map.get("defDate")!=null && map.get("defDate").toString().trim().length()==10)
			{
//				holder.defDate.setText(map.get("defDate").toString().trim().replaceAll("-", "").substring(2));
			}else
			{
				holder.defDate.setText("");
			}
/*			int enable = 0;
			if(map.get("enable")!=null)
			{
				enable = Integer.valueOf(map.get("enable").toString());
			}*/
			String refNumber = "B";
			if(map.get("refNumber")!=null)
			{
				refNumber = map.get("refNumber").toString();
			}
			/*		holder.enable.setBackgroundResource(enable==0?R.drawable.checkbox_no2:R.drawable.checkbox_yes2);
			holder.moveup.setBackgroundResource(R.drawable.arrow_up); 
			holder.movedown.setBackgroundResource(R.drawable.arrow_down); 
			holder.del.setBackgroundResource(R.drawable.guanbi); */
//			holder.enable.setText(enable==0?"○":"●");
//			holder.enable.setTextColor(Color.BLUE);
			holder.refNumber.setText(refNumber);
			holder.refNumber.setTextColor(Color.BLUE);
			holder.moveup.setText("↑");
			holder.movedown.setText("↓");
			holder.del.setText("×");
			holder.del.setTextColor(Color.RED);

			holder.number.setOnClickListener(new View.OnClickListener() { 
				@Override
				public void onClick(View view) {
					Map<String,Object> temp = list.get(position);
					txtNumber.setText(String.valueOf(temp.get("number")));
					txtName.setText(String.valueOf(temp.get("name")));
					txtDefDate.setText(String.valueOf(temp.get("defDate")==null?"":temp.get("defDate")));
				} 
			});
			
			holder.moveup.setOnClickListener(new View.OnClickListener() { 
				@Override
				public void onClick(View view) {
					if(position>=1)
					{
	    				Map<String,Object> temp = list.get(position-1);
	    				list.set(position-1, list.get(position));
	    				list.set(position, temp);
	    				notifyDataSetChanged();
					}
				} 
			});
			holder.movedown.setOnClickListener(new View.OnClickListener() { 
				@Override
				public void onClick(View view) { 
					if(position<=list.size()-2)
					{
	    				Map<String,Object> temp = list.get(position+1);
	    				list.set(position+1, list.get(position));
	    				list.set(position, temp);
	    				notifyDataSetChanged();
					}
				} 
			});
			holder.del.setOnClickListener(new View.OnClickListener() { 
				@Override
				public void onClick(View view) {
					Map<String, Object> map = list.get(position);
					if(map==null || map.get("number")==null || map.get("number").toString().trim().length()==0)
					{
						return;
					}
					String number = map.get("number").toString().trim();
					if("0000001".equals(number) || "0000905".equals(number)
							|| "1399006".equals(number) || "1399102".equals(number) || "0000000".equals(number)
							|| "XAU".equals(number) || "OIL".equals(number))
					{
						return;
					}
					list.remove(position);
					notifyDataSetChanged();
				} 
			});
			
/*			holder.enable.setOnClickListener(new View.OnClickListener() { 
				@Override
				public void onClick(View view) {
					Map<String, Object> map = list.get(position);
					if(map==null || map.get("number")==null || map.get("number").toString().trim().length()==0)
					{
						return;
					}
					String number = map.get("number").toString().trim();
					if("0000001".equals(number) || "0000905".equals(number)
							|| "1399006".equals(number) || "1399102".equals(number) || "0000000".equals(number)
							|| "XAU".equals(number) || "OIL".equals(number))
					{
						return;
					}
					int enable = 1;
					if(map.get("enable")!=null)
					{
						enable = Integer.valueOf(map.get("enable").toString());
						enable = (enable==1?0:1);
					}
					map.put("enable", enable);
					notifyDataSetChanged();
				} 
			});*/
			
			holder.refNumber.setOnClickListener(new View.OnClickListener() { 
				@Override
				public void onClick(View view) {
					Map<String, Object> map = list.get(position);
					if(map==null || map.get("number")==null || map.get("number").toString().trim().length()==0)
					{
						return;
					}

					String refNumber = "B";
					if(map.get("refNumber")!=null)
					{
						refNumber = map.get("refNumber").toString();
						refNumber = (refNumber.equals("B")?"A":"B");
					}
					map.put("refNumber", refNumber);
					notifyDataSetChanged();
				} 
			});


			return convertView; 
		} 

		public class ViewHolder{
			public TextView seq; 
			public TextView name; 
			public TextView number; 
//			public TextView enable; 
			public TextView refNumber; 
			public TextView moveup; 
			public TextView movedown; 
			public TextView del; 
			public TextView defDate; 
		}
	}
}
